#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int N = 110;

int t;
int n, m, r, c;

string s[N];
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin >> t;
    while (t--) {
        cin >> n >> m >> r >> c;
        int b = 0;
        for (int i = 1; i <= n; i++) {
            cin >> s[i];
            s[i] = "#" + s[i];
            for (int j = 1; j <= m; j++) {
                if (s[i][j] == 'B') b++;
            }
        }

        if (b == 0) {
            cout << -1 << endl;
            continue;
        }

        if (s[r][c] == 'B') {
            cout << 0 << endl;
            continue;
        }
        int res = 2;
        for (int i = 1; i <= n; i++) {
            if (s[i][c] == 'B') {
                res = 1;
                break;
            }
        }

        for (int i = 1; i <= m; i++) {
            if (s[r][i] == 'B') {
                res = 1;
                break;
            }
        }
        cout << res << endl;
    }
    return 0;
}